﻿Imports System.Data.SqlClient

Module DbUtils


    Public Function getDbConnection() As SqlConnection
        Try
            Dim cn As New SqlConnection("Data Source=localhost;Initial Catalog=exam;User ID=sa;Password=exam")
            ' cn.ConnectionTimeout = 5  ' 设置默认超时时间为 5 秒
            cn.Open()
            Return cn
        Catch ex As Exception
            Return Nothing
        End Try
    End Function



    'Dim cn As New SqlConnection("Data Source=192.168.2.102;Initial Catalog=exam;User ID=sa;Password=exam")
    'cn.Open()

    'Dim cmd As New SqlCommand("select * from student", cn)
    'Dim dr As SqlDataReader '不能new.应用程序不直接创建 IDataReader 接口的实例，而是创建继承 IDataReader 的类的实例  
    'dr = cmd.ExecuteReader()
    'Dim dt As New DataTable


    ''批量读取数据库的数据
    'Dim strDisplay As String = ""
    'While dr.Read()
    '    strDisplay &= dr("id").ToString + Space(5)
    '    strDisplay &= dr("login_name").ToString + Space(5)
    '    strDisplay &= dr("pwd").ToString + Space(5)
    '    strDisplay &= dr("name").ToString + Space(5)
    '    strDisplay &= dr("cert_id").ToString + Space(5)
    '    strDisplay &= dr("modify_time").ToString + Space(5)
    '    strDisplay &= vbCrLf
    'End While
    'MessageBox.Show(strDisplay)
    'dr.Close()
    'cn.Close()
End Module
